import asyncio
import logging

import json

from pyexpat.errors import messages

import client_utils as utils
import requests

async def main(query:str) -> str|None:
        url = "http://192.168.31.83:18080/v1/chat/completions"
        headers = {"Content-Type": "application/json",
                   "Authorization": "Bearer dogger-f4b216cb08b478f0-202507230905"}

        tools = await utils.get_tools()
        print(tools)
        data = {
            "messages": [
                {"role": "system", "content": "如果用户的问题中包含关键句'--XXX',则直接在tools中寻找相应的tool,并且在使用时去除掉'--XXX'并保留用户的其他语句"},
                {"role": "user", "content": query}],
            "temperature": 0.5,
            "max_tokens": 5120,
            "tools": tools,
        }
        #print(data)
        response = requests.post(url, headers=headers, json=data)
        if response.status_code == 200:
            return response.json()["choices"][0]["message"]
        else:
            logging.log(logging.ERROR, response.text)
            return None

if __name__ == "__main__":
    #TODO: 碰到问题,不懂如何根据问题自动查找rag
    result = asyncio.run(main("这个系统是什么语言编写的? --查找文档"))
   # asyncio.run(utils.get_tool(result))
    print(result)
    if "tool_calls" in result:
        print(result["tool_calls"][0]["function"]["name"].split("&&")[1])